package stack;

import java.util.Stack;

//最小栈
class MinStack {
    public Stack<Integer> stack;
    public Stack<Integer> minStack;

    public MinStack() {
        stack = new Stack<>();
        minStack = new Stack<>();
    }

    //入栈
    public void push(int val) {
        stack.push(val);
        if (minStack.empty()) {
            minStack.push(val);
            return;
        }
        int peekVal = minStack.peek();
        //-1 < 3
        if (val <= peekVal) {
            minStack.push(val);
        }
    }

    //出栈
    public void pop() {
        int val = stack.pop();
        if (val == minStack.peek()) {
                minStack.pop();
        }
    }

    // peek 获得当前普通栈的栈顶元素，不删除
    public int top() {
        return stack.peek();
    }

    //最小栈的peek，每次通过这个方法获取 最小值
    public int getMin() {
        return minStack.peek();
    }
}